package org.example.service;

import org.example.entity.Certification;
import org.example.enums.CertificationStatus;
import org.example.repository.CertificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CertificationService {

    private final CertificationRepository certificationRepository;

    @Autowired
    public CertificationService(CertificationRepository certificationRepository) {
        this.certificationRepository = certificationRepository;
    }

    public List<Certification> findAllCertifications() {
        return certificationRepository.findAll();
    }

    public void approveCertification(Long id) {
        Certification certification = certificationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Certification not found"));
        certification.setStatus(CertificationStatus.APPROVED);
        certification.setReviewDate(new Date());
        certificationRepository.save(certification);
    }

    public void rejectCertification(Long id, String rejectReason) {
        Certification certification = certificationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Certification not found"));
        certification.setStatus(CertificationStatus.REJECTED);
        certification.setReviewDate(new Date());
        certification.setRejectReason(rejectReason);
        certificationRepository.save(certification);
    }
}
